import { legacy_createStore as createStore, applyMiddleware } from "redux";
// import logger from "redux-logger";

const logger = (store) => (next) => (action) => {
    // next(action) -> 相当于 dispatch(action)
    // dispatch 之前
    console.log("更新之前", store.getState(), action);
    const result = next(action);
    // dispatch 之后
    console.log("更新之后", store.getState());
    return result;
};

const reducer = (state = { count: 0 }, action) => {
    switch (action.type) {
        case "add":
            state.count = state.count + (action.payload || 1);
            break;
        case "cut":
            state.count = state.count - 1;
            break;

        default:
            return { ...state };
    }
    return { ...state };
};
// applyMiddleware 中间件 是一个函数
const store = createStore(reducer, applyMiddleware(logger)); // store实例

// store.subscribe(() => {
//     console.log("监听触发了");
//     console.log(store.getState());
// });

export default store;
